*** Outpatient files already subsetted for variables in the botox folder, so let's start there

*** Panelize all HCPCS codes 
* foreach YEAR of numlist 2002/2016{
* 	disp `YEAR'
* 	use hcpcs_cd rprvdpmt rev_dt using /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/botox/synth/100pct/raw/op`YEAR'.dta
* 	gen dt = mofd(rev_dt)

* 	** https://hmsa.com/portal/provider/MM.06.007_Kyphoplasty_and_Vertebroplasty_020117.pdf
* 	* https://www.hcpro.com/HOM-49110-2977/Billing-and-reimbursement-challenges-for-kyphoplasty.html
* 	* https://www.bluecrossnc.com/sites/default/files/document/attachment/services/public/pdfs/medicalpolicy/vertebroplasty_and_kyphoplasty_percutaneous.pdf

* 	collapse (sum) rprvdpmt, by(hcpcs_cd dt)

* 	save /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/kyphon/substitutes/op_`YEAR'_panel.dta, replace 

* }

***************************************************************************
*** Make single clean file
*** Repanelize, incase any repeats in dt 
cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/kyphon/substitutes

use op_2002_panel.dta
foreach YEAR of numlist 2003/2016{
	append using op_`YEAR'_panel.dta
}
rename rprvdpmt pmt_amt 

collapse (sum) pmt_amt, by(hcpcs_cd dt)


*** Group HCPCS codes in the 2-range
gen firstdigit = substr(hcpcs_cd, 1, 1)
keep if firstdigit == "2"
gen hcpcs = real(hcpcs_cd)
drop if hcpcs < 20000
drop if mi(hcpcs)
	* Codes with Fs et c
	* start here https://coder.aapc.com/cpt-codes-range/230

gen code = "general" if hcpcs >= 20100 & hcpcs <= 20999
replace code = "head" if hcpcs >= 21010 & hcpcs <= 21499
replace code = "neckthorax" if hcpcs >= 21501 & hcpcs <= 21899
replace code = "backflank" if hcpcs >= 21920 & hcpcs <= 21936
replace code = "spine" if hcpcs >= 22010 & hcpcs <= 22899
replace code = "abdomen" if hcpcs >= 22900 & hcpcs <= 22999
replace code = "shoulder" if hcpcs >= 23000 & hcpcs <= 23929
replace code = "humeruselbow" if hcpcs >= 22930 & hcpcs <= 24999
replace code = "forearmwrist" if hcpcs >= 25000 & hcpcs <= 25999
replace code = "handfingers" if hcpcs >= 26010 & hcpcs <= 26989
replace code = "pelviship" if hcpcs >= 26990 & hcpcs <= 27299
replace code = "femurknee" if hcpcs >= 27301 & hcpcs <= 27599
replace code = "legankle" if hcpcs >= 27600 & hcpcs <= 27899
replace code = "foottoes" if hcpcs >= 28001 & hcpcs <= 28899
replace code = "casts" if hcpcs >= 29000 & hcpcs <= 29799
replace code = "endoscopy" if hcpcs >= 29800 & hcpcs <= 29999

** Manual edits
replace code = "incisiondrainage" if inlist(hcpcs, 20000, 20005)
drop if mi(code)

*** Collapse by code
collapse (sum) pmt, by(code dt)


*** Temporarily panelize
*** To fill in full panel 
*** And find ones with missing data
encode code, gen(g)
tsset g dt 
tsfill, full
replace pmt_amt = 0 if mi(pmt_amt)

** Sort groups by time, carryforward code
sort g dt 
by g: carryforward code, replace

** Sort groups by reverse time, carryforward code
count if mi(code)
gen temp = 1000 - dt 
sort g temp 
by g: carryforward code, replace 
count if mi(code)
assert (r(N) == 0)
drop temp 


*** Get rid of anything before/after data window
drop if dt < mofd(mdy(1,1,2002))
drop if dt > mofd(mdy(12,31,2016))

save op_panel.dta, replace 
